package com.itheima.easy.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import com.itheima.easy.base.AjaxResult;
import com.itheima.easy.dto.CheckInConfigDto;
import com.itheima.easy.dto.CheckInDto;
import com.itheima.easy.dto.ContractDto;
import com.itheima.easy.service.CheckInConfigService;
import com.itheima.easy.service.CheckInService;
import com.itheima.easy.service.ContractService;
import com.itheima.easy.service.UserService;
import com.itheima.easy.utils.UserThreadLocal;
import com.itheima.easy.vo.CheckInVo;
import com.itheima.easy.vo.SubjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/checkIn")
@Api(tags = "入住")
public class CheckInController {

    @Autowired
    private CheckInService checkInService;

    @Autowired
    private ContractService contractService;

    @Autowired
    private CheckInConfigService checkInConfigService;


    @PostMapping("/create")
    @ApiOperation(value = "申请入住", notes = "传入入住对象")
    public AjaxResult<CheckInVo> createCheckIn(
            @RequestBody @ApiParam(value = "入住对象", required = true) CheckInDto checkInDto) {
        return checkInService.createCheckIn(checkInDto);
    }

    @PostMapping("/review")
    @ApiOperation(value = "评估", notes = "传入入住对象")
    public AjaxResult<CheckInVo> review(
            @RequestBody @ApiParam(value = "入住对象", required = true) CheckInDto checkInDto) {
        return checkInService.review(checkInDto);
    }

    @GetMapping
    @ApiOperation(value = "入住表单查询")
    public AjaxResult<CheckInVo> getCheckIn(
            @RequestParam @ApiParam(value = "入住编码") Long id,
            @RequestParam @ApiParam(value = "处理人ID") String assigneeId,
            @RequestParam @ApiParam(value = "流程状态") Integer flowStatus) {
        return checkInService.getCheckIn(id, assigneeId, flowStatus);
    }

    @PostMapping("/submit")
    @ApiOperation(value = "提交")
    public AjaxResult submitCheckIn(
            @RequestBody @ApiParam(value = "入住对象", required = true) CheckInDto checkInDto) {
        return checkInService.submitCheckIn(checkInDto);
    }

    @PutMapping
    @ApiOperation(value = "驳回")
    public AjaxResult disapprove(
            @RequestParam @ApiParam(value = "入住编码") Long checkInId,
            @RequestParam @ApiParam(value = "驳回消息") String message) {
        return checkInService.disapprove(checkInId, message);
    }

    @PutMapping("/reject")
    @ApiOperation(value = "审核拒绝")
    public AjaxResult auditReject(
            @RequestParam @ApiParam(value = "入住编码") Long checkInId,
            @RequestParam @ApiParam(value = "拒绝原因") String reject) {
        return checkInService.auditReject(checkInId, reject);
    }

    @PutMapping("/revocation")
    @ApiOperation(value = "撤回")
    public AjaxResult revocation(
            @RequestParam @ApiParam(value = "入住编码") Long checkInId,
            @RequestParam @ApiParam(value = "流程状态") Integer flowStatus) {
        return checkInService.revocation(checkInId, flowStatus);
    }

    @PutMapping("/cancel")
    @ApiOperation(value = "撤销")
    public AjaxResult cancel(@RequestParam @ApiParam(value = "入住编码") Long checkInId){
        return checkInService.cancel(checkInId);
    }

    @Autowired
    private UserService userService;

    @ApiOperation(value = "入住管理", notes = "入住管理列表页")
    @GetMapping("/selectByPage")
    public AjaxResult selectByPage(
            @ApiParam(value = "入住编码") @RequestParam(required = false) String checkInCode,
            @ApiParam(value = "姓名") @RequestParam(required = false) String name,
            @ApiParam(value = "身份证号") @RequestParam(required = false) String idCardNo,
            @ApiParam(value = "开始时间") @RequestParam(required = false) Long startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) Long endTime,
            @ApiParam(value = "页码") @RequestParam(required = true,defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量") @RequestParam(required = true,defaultValue = "10") Integer pageSize
    ) {
        String subject = UserThreadLocal.getSubject();
        SubjectVo subjectVo = JSONUtil.toBean(subject, SubjectVo.class);
        String deptNo = subjectVo.getDeptNo();
        Long userId = subjectVo.getId();

        LocalDateTime start = null;
        LocalDateTime end = null;
        if(startTime != null && endTime != null){
            start = LocalDateTimeUtil.of(startTime);
            end = LocalDateTimeUtil.of(endTime);
        }

        return checkInService.selectByPage(checkInCode, name, idCardNo, start, end, pageNum, pageSize,deptNo,userId);
    }

    /**
     * 入住配置
     * @param checkInConfigDto 入住配置
     * @return 操作结果
     */
    @PostMapping
    @ApiOperation(value = "入住配置")
    public AjaxResult checkIn(@RequestBody CheckInConfigDto checkInConfigDto) {
        checkInConfigService.checkIn(checkInConfigDto);
        return AjaxResult.success();
    }

    /**
     * 签约办理
     * @param contractDto 合同信息
     * @return 操作结果
     */
    @PostMapping("/sign")
    @ApiOperation(value = "签约办理")
    public AjaxResult sign(@RequestBody ContractDto contractDto) {
        contractService.sign(contractDto);
        return AjaxResult.success();
    }
}
